package org.darcy.controller.org;

import java.util.List;

import org.darcy.controller.BaseController;
import org.darcy.entity.org.Company;
import org.darcy.framework.model.R;
import org.darcy.service.org.CompanyService;
import org.darcy.utils.common.ReturnCode;
import org.darcy.utils.vo.ReturnVo;
import org.darcy.vo.CheckExistVo;
import org.darcy.vo.org.OrgTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "CompanyController接口：企业信息")
@RestController
@RequestMapping("/org/company")
@Slf4j
public class CompanyController extends BaseController<Company> {
	
	private final String info_prefix = "[企业信息模块]";

	@Autowired
	private CompanyService service;
	
	@Operation(summary = "企业分页列表")
	@PostMapping(value = "list")
	public R list(@RequestBody Company param) {
		log.debug("{}-列表数据", info_prefix);
		List<Company> data = service.list(param);
		return R.ok().data(data);
	}

	@Operation(summary = "企业详情")
	@GetMapping("/view/{id}")
	public R view(@PathVariable Integer id) {
		log.debug("{}-查看企业信息", info_prefix);
		Company data = service.getById(id);
		return R.ok().data(data);
	}

	@Operation(summary = "新增企业")
	@PostMapping("add")
	public R add(@RequestBody Company record) {
		log.debug("{}-新增企业: {}", info_prefix, record);
		service.insert(record);
		return R.ok("新增成功").data(record);
	}

	@Operation(summary = "根据ID删除企业")
	@PostMapping("/del/{id}")
	public R del(@PathVariable Integer id) {
		log.debug("{}-根据ID删除", info_prefix, id);
		service.delete(id);
		return R.ok("删除成功");
	}

	@Operation(summary = "更新企业")
	@PostMapping("update")
	public R update(@RequestBody Company record) {
		log.debug("{}-保存信息: {}", info_prefix, record);
		service.update(record);
		return R.ok("更新成功");
	}

	@Operation(summary = "查看属性")
	@GetMapping("/model")
	public Company model() {
		return null;
	}

	@PostMapping(value = "/checkEntityExist", produces = "application/json")
	protected R checkEntityExist(@RequestBody CheckExistVo checkExistVo) {
		return this.checkExist(service, checkExistVo);
	}

	@Operation(summary = "获取公司树")
	@GetMapping(value = "/getCompanyTree", produces = "application/json")
	public ReturnVo<List> getCompanyTree() {
		ReturnVo<List> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
		List<OrgTreeVo> datas = service.getCompanyTree(null);
		returnVo.setData(datas);
		return returnVo;
	}

	@Operation(summary = "添加或者修改公司")
	@PostMapping(value = "/saveOrUpdate", produces = "application/json")
	public ReturnVo<String> saveOrUpdate(@RequestBody Company company) {
		ReturnVo<String> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
		service.saveOrUpdate(company, this.getLoginUser());
		return returnVo;
	}

	@Operation(summary = "删除公司")
	@PostMapping(value = "/delete", produces = "application/json")
	public ReturnVo<String> delete(@RequestBody List<String> ids) {
		ReturnVo<String> returnVo = service.deleteByIds(ids);
		return returnVo;
	}


}
